package cn.net.Stream;
/*字符（字节）文件复制案例
*
* 复制：需要两个路径，需要同时存在【高效输入+高效输出】
* 字符：Reader--BufferedReader--FileReader  +   Writer---BufferedWriter--FileWriter
* 字节: InputStream--BufferedInputStream--FileInputStream   +   OutputStream--Buffered--File

 * */
import java.io.*;
import java.util.Scanner;


public class TestCopyFile {
    public static void main(String[] args) {
        //1.提示
        System.out.println("请输入源文件路径：");
        String f = new Scanner(System.in).nextLine();//复制内容 Reader读入
        System.out.println("请输入目标文件路径：");
        String t = new Scanner(System.in).nextLine();//复制到哪儿去 Writer写出
        //2.调用方法，传入两个参数
        //ZFcopy(f,t);//字符文件复制
        ZJcopy(f,t);//字节文件复制
    }

    private static void ZJcopy(String f, String t) {
        InputStream in = null;//字符输入流
        OutputStream  out = null;//字符输出流

        try {
            //查看读取
            in = new BufferedInputStream(new FileInputStream(f));//传入源文件路径参数f
            //复制写出
            out = new BufferedOutputStream(new FileOutputStream(t));//传入目标文件路径参数f
            int b ;
            while((b=in.read())!=-1){//-1表示没有数据输出，结束循环
                System.out.println(b);//边读入进来
                out.write(b);//边写出去
            }

            System.out.println("恭喜你文件复制成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("抱歉，复制失败");
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void ZFcopy(String f, String t) {
        Reader in = null;//字符输入流
        Writer out = null;//字符输出流

        try {
            //查看读取
            in = new BufferedReader(new FileReader(f));//传入源文件路径参数f
            //复制写出
            out = new BufferedWriter(new FileWriter(t));//传入目标文件路径参数f
           int b ;
           while((b=in.read())!=-1){//-1表示没有数据输出，结束循环
               System.out.println(b);//边读
               out.write(b);//边写
           }

            System.out.println("恭喜你文件复制成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("抱歉，复制失败");
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
